import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:seedfour/app/constants.dart';
import 'package:seedfour/app/theme.dart';
import 'package:seedfour/toolkit/typedef/function.dart';

import '../state.dart';

class HomeNavigation extends StatelessWidget {
  /// 数据源
  final HomeState data;

  /// 点击功能按钮
  final ParamSingleCallback<String> onClickButton;

  const HomeNavigation({
    Key? key,
    required this.data,
    required this.onClickButton,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return _buildBg(children: [
      _buildButtonBg(builder: (item) {
        return [
          // SVG 图标
          Image.asset(
            item.iconPath,
            width: item.iconWidth,
            height: item.iconHeight,
          ),

          SizedBox(height: kSpaceTiny),

          // 标题
          Text(
            item.name,
            style: TextStyle(
                color: AppColor.colorFontBlack, fontSize: kFontSizeSmall),
          ),
        ];
      }),
    ]);
  }

  Widget _buildButtonBg({
    required List<Widget> Function(NavigationButtonItemInfo item) builder,
  }) {
    return Obx(
      () => Wrap(
        alignment: WrapAlignment.spaceEvenly,
        spacing: kSpaceLarge,
        children: List.generate(
          data.navigateIconList.length,
          (index) => TextButton(
            onPressed: () =>
                onClickButton(data.navigateIconList[index].businessType),
            style: ButtonStyle(
              shape: WidgetStateProperty.all(const RoundedRectangleBorder(
                  borderRadius: BorderRadius.zero)),
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: builder(data.navigateIconList[index]),
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildBg({required List<Widget> children}) {
    return Container(
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(kRadiusSmall)),
      margin: EdgeInsets.symmetric(horizontal: kSpaceSmall),
      padding: EdgeInsets.symmetric(vertical: kSpaceSmall),
      child: Column(
        children: children,
      ),
    );
  }
}
